import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:xiandou/Request/DioManager.dart';
import 'package:xiandou/Request/NWApi.dart';
import 'package:xiandou/Request/NWMethod.dart';

class ListViewScreen extends StatefulWidget {
  @override
  State<StatefulWidget> createState() =>  ListViewScreenState();
}

class ListViewScreenState extends State<ListViewScreen> {
  @override
  void initState() {
    // TODO: implement initState
    _getGoldListData(true, 1);
    _getCurrentUserInfo ();
    super.initState();
  }

  var _goldListData = [];
  String balanceStr = '0';
  void _getCurrentUserInfo (){
    DioManager().request(NWMethod.GET, NWApi.getUserInfoApi,params: null,success: (data){
      print(data);
      setState(() {
        balanceStr = data['balance'].toString();
      });
    },error: (error){
      Fluttertoast.showToast(msg: error.message);
    });
  }

  void _getGoldListData(bool isRefresh,int page){
    var params = {'page':page.toString(),};
    DioManager().request(NWMethod.GET, NWApi.getGoldListApi,params:params,success: (data){
      setState(() {
        _goldListData = data;
      });
      print(data);
    },error: (error){
      Fluttertoast.showToast(msg: error.message);
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
        appBar: CupertinoNavigationBar(
          backgroundColor: Colors.white,
          middle: const Text('金币记录'),
        ),
        body: Container(
//        height: MediaQuery.of(context).size.height,
//        width: MediaQuery.of(context).size.width,
          color: Color(0xffF8F8F8),
          child: Column(
            children: <Widget>[
              Container(
                height: 98,
                width: MediaQuery.of(context).size.width-20,
                margin: const EdgeInsets.fromLTRB(10, 9, 10, 5),
                decoration: new BoxDecoration(
                    color: Color(0xffFF5236),
                    borderRadius: BorderRadius.all(Radius.circular(5))
                ),
                child: Column(
                  children: <Widget>[
                    Padding(
                      child:Text('账户总额（闲豆）',style: TextStyle(color: Color(0xffFFFFFF)),),
                      padding: const EdgeInsets.only(top: 25),
                    ),
                    Text(balanceStr,style: TextStyle(color: Color(0xffFFFFFF),fontWeight: FontWeight.bold,fontSize: 21),)
                  ],
                ),
              ),
              Expanded(
                child: Container(
                  decoration: new BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.all(Radius.circular(5)),
                  ),
                  margin: const EdgeInsets.fromLTRB(10, 10, 10, 0),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Container(
                        margin: const EdgeInsets.only(left: 23.5,top: 25),
                        child: Text('收入明细',style: TextStyle(fontSize: 17,color: Colors.black),),
                      ),
                      Container(
                        margin: const EdgeInsets.only(left: 23.5,top: 5),
                        height: 2,
                        width: 70,
                        color: Color(0xffFF5236),
                      ),
                      Expanded(
                        child: ListView.separated(itemBuilder: (BuildContext context,int index){
                          return ListTile(
                            title: Text(_goldListData[index]['description']),
                            subtitle: Container(
                              margin: const EdgeInsets.only(top: 10),
                              child: Text(_goldListData[index]['createTime']),
                            ),
                            trailing: Text(_goldListData[index]['amount'].toString(),style: TextStyle(color: Color(0xffFF5236),fontSize: 16,fontWeight: FontWeight.w600),),
                          );
                        },itemCount: _goldListData.length,separatorBuilder: (BuildContext context,int index){
                          return Divider(color: Color(0xffEEEEEE));
                        },),
                      )
                    ],
                  ),
                ),
              )
            ],
          ),
        )
    );
  }
}